import { Injectable } from '@nestjs/common';
import { Classification, Prisma } from '@prisma/client';
import { PrismaService } from 'src/db/prisma.service';
import { requestPromise } from 'src/interface';

@Injectable()
export class ClassificationService {
  constructor(private prisma: PrismaService) {}

  async findOne(
    classificationWhereUniqueInput: Prisma.ClassificationWhereUniqueInput,
  ): Promise<Classification | null> {
    return this.prisma.classification.findUnique({
      where: {
        id: +classificationWhereUniqueInput.id,
      },
    });
  }

  async findAll(params: {
    pageNum?: number;
    pageSize?: number;
    cursor?: Prisma.ClassificationWhereUniqueInput;
    where?: Prisma.ClassificationWhereInput;
    orderBy?: Prisma.ClassificationOrderByWithRelationInput;
  }): Promise<requestPromise> {
    const { pageNum, pageSize, cursor, where, orderBy } = params;
    console.log(params, 'params');
    const totalUsersCount = await this.prisma.classification.count({
      where: { ...where },
    });
    const classifications = await this.prisma.classification.findMany({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
      cursor,
      where,
      orderBy,
    });
    return {
      code: 200,
      message: 'success',
      data: {
        list: classifications,
        total: totalUsersCount,
      },
    };
  }

  async create(
    data: Prisma.ClassificationCreateInput,
  ): Promise<Classification> {
    return this.prisma.classification.create({
      data,
    });
  }

  async update(params: {
    where: Prisma.ClassificationWhereUniqueInput;
    data: Prisma.ClassificationUpdateInput;
  }): Promise<Classification> {
    const { where, data } = params;
    return this.prisma.classification.update({
      data,
      where,
    });
  }

  async delete(
    where: Prisma.ClassificationWhereUniqueInput,
  ): Promise<Classification> {
    return this.prisma.classification.delete({
      where: {
        id: +where.id,
      },
    });
  }
}
